﻿

namespace WordBibliographyAddin
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;

    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            WordApplication.Instance = this.Application;
            CopyBibliographyStylesToWordDirectory();

        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {

        }

        private void CopyBibliographyStylesToWordDirectory()
        {
            int count = 0;
            Assembly current = Assembly.GetExecutingAssembly();

            string path = Utils.GetWordBibliographyStylesFolder();
            if (Directory.Exists(path))
            {

                string[] resources = current.GetManifestResourceNames();

                foreach (string resource in resources)
                {
                    if (Path.HasExtension(resource) && Path.GetExtension(resource).Equals(".xsl", StringComparison.CurrentCultureIgnoreCase))
                    {
                        using (Stream str = current.GetManifestResourceStream(resource))
                        {
                            string newFileName = resource.Replace(current.GetName().Name, string.Empty);
                            newFileName = newFileName.Replace(".NewStyles.", string.Empty);

                            string newFile = Path.Combine(path, newFileName);
                            if (!File.Exists(newFile))
                            {
                                if (Utils.HasAdminPrivileges())
                                {
                                    using (FileStream outFile = File.OpenWrite(newFile))
                                    {
                                        str.CopyTo(outFile);
                                        str.Flush();
                                    }
                                    count++;
                                }
                                else
                                {
                                    string text = "{0} does not have permission to copy new bibliography styles into the folder: {1}" +
                                            ".\n\nTo fix this issue, please grant Microsoft Word administrator privileges.";

                                    Utils.ShowKryptonDialog("Permissions are not granted.", text, MessageBoxIcon.Error, "Biblio!", path);
                                    break;
                                }
                            }
                        }
                    }
                }
            }


            if (count > 0)
            {
                string text = "{0} has copied {1} new bibliography styles into the folder: {2}";
                Utils.ShowKryptonDialog("New Bibliography Styles are added.", text, MessageBoxIcon.Information, "Biblio!", count, path);
            }

        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
